public static void initTxt(String string) throws IOException {
        String path = new File(string).getAbsolutePath();
        FileWriter fw = new FileWriter(path, false);
        fw.write("");
        fw.flush();
        fw.close();
	}
	
	public static void writeMostWord(String infilename,String outfilename) throws IOException {//按照格式输出结果
		String outpath = new File(outfilename).getAbsolutePath();
        FileWriter fw = new FileWriter(outpath, true);
        TreeMap<String, Integer> tm = wordMap(infilename);
		if(tm != null && tm.size()>=1)
		{		  
			List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(tm.entrySet());
			// 通过比较器来实现排序
			Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
				public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
					//treemap默认按照键的字典序升序排列的，所以list也是排过序的，在值相同的情况下不用再给键升序排列		
					// 按照值降序排序 
					return o2.getValue().compareTo(o1.getValue());
				}
			});   
			int i = 1;
			String key = null;
			Integer value = null;
			for (Map.Entry<String, Integer> mapping : list) {
				key = mapping.getKey();
				value = mapping.getValue();
				System.out.print("<" + key + ">: " + value + '\n');
				fw.write("<" + key + ">: " + value + '\n');
				//只输出前10个
				if (i == 10) {
					break;
				}
				i++;
			}
		}
		fw.close();
	}
	
	  public static void writeResult(String infilename,String outfilename) throws IOException {
		  //实现输出
	        File file = new File(infilename);
	        if (file.exists()) {	
	        	initTxt(outfilename);
	        	String outpath = new File(outfilename).getAbsolutePath();
	        	FileWriter fw = new FileWriter(outpath, true);
	        	int charactersNum = charNum(infilename);
	        	int wordsNum = wordNum(infilename);
	        	int linesNum = lineNum(infilename);
	        	System.out.print("characters: " + charactersNum + '\n');
	        	System.out.print("words: " + wordsNum + '\n');
	        	System.out.print("lines: " + linesNum + '\n');
	        	fw.write("characters: " + charactersNum + '\n');
	        	fw.write("words: " + wordsNum + '\n');
	        	fw.write("lines: " + linesNum + '\n');
	        	fw.flush();
	        	writeMostWord(infilename,outfilename);
	        	if (fw != null) {
	        		fw.close();
	        	}
	        }
			else {
				System.out.println("文件不存在");
			}
	    }
	  
	  public static int charNum(String filename) throws IOException  {
		  //统计文件的字符数
			int num = 0;		
			BufferedReader br = new BufferedReader(new FileReader(filename));
			int value = -1;// EOF标记文件末尾
			while ((value = br.read()) != -1) {
				//判断是否读到了文件末尾
				if (value > 0 && value < 128 && value != 13) {
					//不统计回车符，ASCII正常十进制范围0~127
					num ++;
				}			
			}
			br.close();		
			return num;
		}

		public static int wordNum(String filename) throws IOException  {
			//统计单词总数
			int num = 0;			
			BufferedReader br = new BufferedReader(new FileReader(filename));
			String separator = "[^A-Za-z0-9]";//分隔符
			String regex = "^[A-Z a-z]{4,}[0-9]*$"; //正则判断每个数组中是否存在有效单词
			Pattern p = Pattern.compile(regex);
			Matcher m = null;
			String line = null;
			String[] array = null;
			while ((line  = br.readLine()) != null) {
	            line = line.replaceAll("[(\\u4e00-\\u9fa5)]", "");// 用空格替换汉字
	            line = line.replaceAll(separator, " "); // 用空格替换分隔符
	            array = line.split("\\s+"); // 按空格分割                     
	            for (int i = 0;i<array.length;i++) { 
	            	m = p.matcher(array[i]);
	            	if (m.matches()) {
	            		num++;
	            	}
	            }
			}						
			br.close();		
			return num;
		}	

		public static TreeMap<String, Integer> wordMap(String filename) throws IOException {
			//单词-频率作为键值对保存在TreeMap中	
	        TreeMap<String, Integer> tm = new TreeMap<String, Integer>();				
			BufferedReader br = new BufferedReader(new FileReader(filename));
			String separator = "[^A-Za-z0-9]";//分隔符
			String regex = "^[A-Za-z]{4,}[0-9]*$"; // 正则判断每个数组中是否存在有效单词
			Pattern p = Pattern.compile(regex);
			String str = null;
			Matcher m = null;
			String line = null;
			String[] array = null;
			while ((line  = br.readLine()) != null) {
	            line = line.replaceAll("[(\\u4e00-\\u9fa5)]", "");// 过滤汉字
	            line = line.replaceAll(separator, " "); // 用空格替换分隔符
	            array = line.split("\\s+"); // 按空格分割                     
	            for (int i = 0;i<array.length;i++) { 
	            	m = p.matcher(array[i]);
	            	if (m.matches()) {
	            		str = array[i].toLowerCase();                
	                    if (!tm.containsKey(str)) {
	                        tm.put(str, 1);
	                    } else {
	                        int count = tm.get(str) + 1;
	                        tm.put(str, count);
	                    }
	            	}
	            }
			}						
			br.close();		
			return tm;
		}

	 	public static int lineNum(String filename) throws IOException  {
	 		//统计行数
			int num = 0;			
			BufferedReader br = new BufferedReader(new FileReader(filename));
			String line = null;
			while ((line  = br.readLine()) != null) {
				if (line.trim().length() != 0) {
					num ++;					
				}
			}
			br.close();		
			return num;
		}	
}
	